package ita.class1.group2.supermarket.merchant.controller;

import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.auth.SecurityUtils;
import ita.class1.group2.supermarket.common.auth.Subject;
import ita.class1.group2.supermarket.common.jms.JmsSender;
import ita.class1.group2.supermarket.common.pojo.MessageResultCode;
import ita.class1.group2.supermarket.common.pojo.ResponseMessage;
import ita.class1.group2.supermarket.common.util.JsonUtil;
import ita.class1.group2.supermarket.merchant.pojo.User;
import ita.class1.group2.supermarket.merchant.util.CovertParamsToBean;

import java.util.Map;

@Component
public class StoreFormController {

    private static final String NO_LOGIN = "请先登录";


    @Autowired
    SecurityUtils securityUtils;

    @Autowired
    JmsSender jmsSender;

    @RequestMapping("/stormForm/send")
    public String getStoreForm(String jsonData) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage();
        Subject subject = securityUtils.getSubject();
        if(subject.isLogged()){
            Map<String, Object> paramsMap = JsonUtil.json2map(jsonData);
            paramsMap.put("user",subject.getAuthorizingInfo());
            String storeFormMsg = JsonUtil.obj2json(CovertParamsToBean.covertParamsToStFo(paramsMap));
            jmsSender.sendMessage(JmsSender.Queue.LANCE_QUEUE,storeFormMsg);
            responseMessage.setResultCode(MessageResultCode.SUCCESS);
        }
        else {
            responseMessage.setResultCode(MessageResultCode.UNAUTHORIZED);
            responseMessage.setMessage(NO_LOGIN);
        }

        return JsonUtil.obj2json(responseMessage);
    }

}
